package dongshi.daddy;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
演示GC日志生成与解读
*/
public class GCLogAnalysis {
    private static Random random = new Random();
    public static void main(String[] args) {
        // 136****b***
        System.out.println(toStar1("1363635bcde", "kiuk"));
        // 中间加星 136****bcde
        // System.out.println(toStar1("1363635bcde", "(.{3}).{4}(.{4})"));
        // 男后一个字符变成星,男前不知道有多少个字符，写(.)
    //    System.out.println(toStar1("1363635b男de", "(.)(男).{1}(.{1})"));
      // 男后3个字符变成星,男前不知道有多少个字符，写(.),最后不知道有多个字符，写(.)
        // System.out.println(toStar1("1363635b男dedfdafwefwegweg", "(.)(男).{3}(.)"));
      // 开始2个字符变成星，男后3个字符变成星,男前不知道有多少个字符，写(.),最后不知道有多个字符，写(.)
        // System.out.println(toStar1("1363635b男dedfdafwefwegweg", ".{2}(.)(男).{3}(.)"));
       //System.out.println(toStar1("1363635b男dedfdafwefwegweg", ".{2}(.男).{3}(.)"));
        // SELECT tbl_jh_form_business_db23_olLWowY0.2eaeda5772f24f448c471af82c4dcc90,tbl_jh_form_business_db23_olLWowY0.44d1257c928e4ff78770a8c011a9a721,tbl_jh_form_business_db23_olLWowY0.BUSINESS_ID,'tbl_jh_form_business_db23_olLWowY0' FROM tbl_jh_form_business_db23_olLWowY0 tbl_jh_form_business_db23_olLWowY0 WHERE 1=1  and tbl_jh_form_business_db23_olLWowY0.COMM_RECORD_IDENTY = 1   order by tbl_jh_form_business_db23_olLWowY0.COMM_CREATE_TIME desc

        // <option value="身份证号" <c:if test="${displayManner.regexType eq '身份证号'}">selected</c:if>>身份证号(410*********151X)</option>
        // String str = "41072719900115151X";
        // System.out.println(str.replaceAll("(.{3}).{10}(.)","$1**********"));
        // System.out.println(toStar1(str, "(.{3}).{10}(.)"));
        // <option value="手机号" <c:if test="${displayManner.regexType eq '手机号'}">selected</c:if>>手机号(136****8993)</option>
        // str = "13605178993";
        // System.out.println(toStar1(str, "(.{3}).{4}(.)"));
        // <option value="邮箱" <c:if test="${displayManner.regexType eq '邮箱'}">selected</c:if>>邮箱(3035*****@qq.com)</option>
        // str = "9@163.com";
        // System.out.println(toStar1(str, "(.{1}).{11}(@)(.)"));
        // <option value="地址" <c:if test="${displayManner.regexType eq '地址'}">selected</c:if>>地址(数字替换成*)</option>
        // System.out.println(toStar1(str, "(\\d)"));
        // str = "北京市海淀南路89号西直门小区6栋3344";
        // removeLetter(str);
        // (.{1}).{1}(.)
        // System.out.println(toStar1(str, "(.{1}).{1}(.{1000})"));
// str = "北京市海淀南路89号西直门小区6栋3344";
// System.out.println(toStar1(str, "(\\d)"));
//        System.out.println(emailToStar(str));
    }

    
    public static String emailToStar(String value){
        char firstChar = value.charAt(0);
        int atPos = value.indexOf("@");
        String beforeAt = value.substring(0, atPos);
        String afterAt = value.substring(atPos, value.length());

        int starNum = beforeAt.length() - 1;
        String resutlStr = firstChar + "";
        for (int i = 0; i < starNum; i++) {
            resutlStr += "*";
        }
        resutlStr += afterAt;
        return resutlStr;
    }

    public static void removeLetter(String value){
        Pattern p = Pattern.compile("[\\d]");
        Matcher matcher = p.matcher(value);
        String result = matcher.replaceAll("*");
        System.out.println(result);
    }


    // return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1*****$2");
    public static String toStar(String content) {
        // return content.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1*****$2");
        return content.replaceAll(".{2}(.)(男).{3}(.)","$1");

    }

    public static String toStar2(String content, String regString) {
        // return content.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1*****$2");
        // return content.replaceAll("(.{3}).{4}(.{1}).{3}","$1***$2***");
        char leftBracket =  '(';
        char rightBracket =  ')';
        String replaceStr = "";
        boolean foundLeftBracket = false;
        boolean foundRightBracket = false;
        int groupNum = 1;
        for (int i = 0; i < regString.length(); i++) {
            char curChar = regString.charAt(i);
            // 如果是(则直接匹配到最后的)
            if (curChar == '(') {
                // 获取)的位置
                // int rightBracketIndx = regString.lastIndexOf(")", i);
                int rightBracketIndx = regString.indexOf(")", i);
                // 往后走
                // i = rightBracketIndx + 1;
                i = rightBracketIndx;
                replaceStr += "$" + (groupNum++);
            } else { // 不是(的情况
                // {的位置
                int leftBigBracketPos = regString.indexOf("{", i);
                // }的位置
                int rightBigBracketPos = 4;
                try {
                    rightBigBracketPos = regString.indexOf("}", i);
                } catch (Exception e) {
                }
                int startNum  = Integer.valueOf(regString.substring(leftBigBracketPos + 1, rightBigBracketPos));
                // while (startNum-- >= 0) {
                while (startNum-- > 0) {
                    replaceStr += "*";
                }
                i = rightBigBracketPos;
            }
        }

        // Pattern pattern = Pattern.compile(regString);
        // Matcher matcher = pattern.matcher(content);
        // if (matcher.find()){
        //     System.out.println(matcher.group(0));
        //     System.out.println(matcher.group(1));
        //     System.out.println(matcher.group(2));
        // }
        return content.replaceAll(content, replaceStr);
    }

    public static String toStar1(String content, String regString) {
        // return content.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1*****$2");
        // return content.replaceAll("(.{3}).{4}(.{1}).{3}","$1***$2***");
        char leftBracket =  '(';
        char rightBracket =  ')';
        String replaceStr = "";
        boolean foundLeftBracket = false;
        boolean foundRightBracket = false;
        int groupNum = 1;
        for (int i = 0; i < regString.length(); i++) {
            char curChar = regString.charAt(i);
            // 如果是(则直接匹配到最后的)
            if (curChar == '(') {
                // 获取)的位置
                // int rightBracketIndx = regString.lastIndexOf(")", i);
                int rightBracketIndx = regString.indexOf(")", i);
                // 往后走
                // i = rightBracketIndx + 1;
                i = rightBracketIndx;
                replaceStr += "$" + (groupNum++);
            } else { // 不是(的情况
                // {的位置
                int leftBigBracketPos = regString.indexOf("{", i);
                // }的位置
                int rightBigBracketPos = regString.indexOf("}", i);
                int startNum  = Integer.valueOf(regString.substring(leftBigBracketPos + 1, rightBigBracketPos));
                // while (startNum-- >= 0) {
                while (startNum-- > 0) {
                    replaceStr += "*";
                }
                i = rightBigBracketPos;
            }
        }
        return content.replaceAll(regString, replaceStr);
    }

    // 生成对象
    // 创建不同的对象模拟业务中真实对象的创建
    private static Object generateGarbage(int max) {
        int randomSize = random.nextInt(max);
        int type = randomSize % 4;
        Object result = null;
        switch (type) {
            case 0:
                result = new int[randomSize];
                break;
            case 1:
                result = new byte[randomSize];
                break;
            case 2:
                result = new double[randomSize];
                break;
            default:
                StringBuilder builder = new StringBuilder();
                String randomString = "randomString-Anything";
                while (builder.length() < randomSize) {
                    builder.append(randomString);
                    builder.append(max);
                    builder.append(randomSize);
                }
                result = builder.toString();
                break;
        }
        return result;
    }
}